package com.irm.service;

import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.PaginationUtil;
import com.irm.common.Bm04ExceptionConstant;
import com.irm.dto.DraftOperDTO;
import com.irm.dto.MeetingCodeQueryDTO;
import com.irm.dto.MockInfoQueryDTO;
import com.irm.dto.MsgLogQueryDTO;
import com.irm.pojo.IrmMeetingCode;
import com.irm.pojo.IrmMockInfo;
import com.irm.pojo.IrmMsgLog;
import com.irm.repository.IrmMeetingCodeRepository;
import com.irm.repository.IrmMockInfoRepository;
import com.irm.repository.IrmMsgLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-01-20
 */
@Service
@Transactional(transactionManager = "irmDbTransactionManage")
public class DraftOperServiceImpl implements IDraftOperService{

    @Autowired
    IrmMeetingCodeRepository meetingCodeRepository;
    @Autowired
    IrmMockInfoRepository mockInfoRepository;
    @Autowired
    IrmMsgLogRepository msgLogRepository;

    /**
     * 操作DraftMock
     *
     * @param draftOperDTO 操作信息
     * @return 操作记录数
     * @throws GadgetsServiceException 服务异常
     */
    @Override
    @Transactional(transactionManager = "irmDbTransactionManage", propagation = Propagation.REQUIRED)
    public int operationDraftMock(DraftOperDTO draftOperDTO) throws GadgetsServiceException {
        MeetingCodeQueryDTO queryDTO = new MeetingCodeQueryDTO();
        queryDTO.setCpesQueueOne("CPES"+draftOperDTO.getCardNo()+"_1");
        queryDTO.setQmrtName(draftOperDTO.getQmgrId());
        List<IrmMeetingCode> meetingCodeList = meetingCodeRepository.selectMeetingCodeInfo(queryDTO);
        if(null!=meetingCodeList && meetingCodeList.size()>0){
            IrmMeetingCode meetingCode = meetingCodeList.get(0);
            // 根据ID锁定一下接入点表信息，用于后面防止并发
            int i = meetingCodeRepository.updateMeetingCodeForLock(meetingCode);
            MockInfoQueryDTO mockInfoQueryDTO = new MockInfoQueryDTO();
            // 唯一性条件：Draft_no + Org_Draft_no + Qmgr_id + Meeting_code + Member_id
            mockInfoQueryDTO.setDraftNo(draftOperDTO.getSendDraft());
            mockInfoQueryDTO.setOrgDraftNo(draftOperDTO.getOriDraftNo());
            mockInfoQueryDTO.setQmgrInfoId(draftOperDTO.getQmgrId());
            mockInfoQueryDTO.setMemberId(draftOperDTO.getMemberId());
            mockInfoQueryDTO.setMeetingCode(meetingCode.getMeetingCode());
            List<IrmMockInfo> irmMockInfoList = mockInfoRepository.selectDraftMockInfo(mockInfoQueryDTO);
            if(null!=irmMockInfoList && irmMockInfoList.size()>0){
                // 如果MockInfo信息存在，做数据更新。这种场景下是修改Mock_type字段的
                IrmMockInfo mockInfo = irmMockInfoList.get(0);
                mockInfo.setMockType(draftOperDTO.getMockType());
                mockInfoRepository.updateMockType(mockInfo);
            }else{
                // 新增MockInfo信息
                IrmMockInfo irmMockInfo = new IrmMockInfo();
                irmMockInfo.setDraftNo(draftOperDTO.getSendDraft());
                irmMockInfo.setOriDraftNo(draftOperDTO.getOriDraftNo());
                irmMockInfo.setMeetingCode(meetingCode.getMeetingCode());
                irmMockInfo.setQmgrInfoId(draftOperDTO.getQmgrId());
                irmMockInfo.setMockType(draftOperDTO.getMockType());
                irmMockInfo.setMemberId(draftOperDTO.getMemberId());
                irmMockInfo.setMemberName(draftOperDTO.getCardNo());
                mockInfoRepository.insertMockInfo(irmMockInfo);
            }
        }else{
            // 如果接入点信息不存在，就抛出异常
            throw new GadgetsServiceException(Bm04ExceptionConstant.MEETING_CODE_NOT_EXIST,queryDTO.getCpesQueueOne());
        }
        return 0;
    }

    /**
     * 查询MockInfo
     * @param mockInfoQueryDTO 查询条件
     * @param pagination       分页条件
     * @return 查询条件
     * @throws GadgetsServiceException 服务异常
     */
    @Override
    public List<IrmMockInfo> selectMockInfo(MockInfoQueryDTO mockInfoQueryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<IrmMockInfo> mockInfos = mockInfoRepository.selectDraftMockInfo(mockInfoQueryDTO);
        PaginationUtil.getPaginationResultInfo(mockInfos, pagination);
        return mockInfos;
    }

    /**
     * 查询MsgLog
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     * @return 查询结果
     * @throws GadgetsServiceException 服务异常
     */
    @Override
    public List<IrmMsgLog> selectMsgLog(MsgLogQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<IrmMsgLog> msgLogs = msgLogRepository.selectMsgLog(queryDTO);
        PaginationUtil.getPaginationResultInfo(msgLogs, pagination);
        return msgLogs;
    }

    /**
     * 查询MsgLog
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     * @return 查询结果
     * @throws GadgetsServiceException 服务异常
     */
    @Override
    public List<IrmMsgLog> selectMsgContent(MsgLogQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<IrmMsgLog> msgLogs = msgLogRepository.selectMsgContent(queryDTO);
        PaginationUtil.getPaginationResultInfo(msgLogs, pagination);
        return msgLogs;
    }
}
